package ch9;

public class StringClass {

	public static void main(String[] args) {
		String s = new String("abc");
		//java的字符串对象是不可以修改的，每次修改字符串都会分配新的内存空间
		//String类的修改性能很低
		s = new String("asdfsaadsaffsdafsff");
		s = new String("asfdadsa");
		s = new String("asfdadsas1");

		//带缓冲的字符串类 - StringBuffer多线程安全；StringBuilder是多线程不安全的；
		//面试题：简述String，StringBuffer和StringBuilder的异同点
		StringBuilder buffer = new StringBuilder();
		//StringBuffer buffer = new StringBuffer();
		buffer.append(false);
		buffer.append("Hello");
		buffer.insert(5, "ABCDEF");
		
		int cap = buffer.capacity();//字符串缓冲的容量
		int len = buffer.length();//数据长度
		
		
		
		System.out.println(cap+","+len );
		System.out.println(buffer.toString());
		System.out.println("=====================");
		
		buffer.insert(0, "A");
		cap = buffer.capacity();//字符串缓冲的容量
		len = buffer.length();//数据长度
		System.out.println(cap+","+len );
		System.out.println(buffer.toString());
		System.out.println("=====================");
		
		buffer.deleteCharAt(0);
		buffer.delete(0, 1);
		buffer.trimToSize();//释放没有使用的空间容量
		cap = buffer.capacity();//字符串缓冲的容量
		len = buffer.length();//数据长度
		System.out.println(cap+","+len );
		System.out.println(buffer.toString());
		System.out.println("==========================");
		
		//buffer.replace(0, buffer.length(), "A");
		buffer.replace(0, 1, "Q");
		buffer.reverse();//字符串反向输出
		System.out.println(buffer.toString());
	}

}
